/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package heartsinjava2;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Random;
import javax.swing.*;


/**
 * A basic extension of the javax.swing.JApplet class
 */


public class GamePlayDemo extends JApplet
{

    private Deck cardDeck;
    private final String directory = "cards\\";

    javax.swing.JButton cardDisplayLabel = new javax.swing.JButton();
    javax.swing.JButton drawCardBtn = new javax.swing.JButton();
    javax.swing.JButton newDeckBtn = new javax.swing.JButton();
    javax.swing.JLabel cardsLeftLbl = new javax.swing.JLabel();

    JButton[] P1_listcard ;
    JButton[] P2_listcard ;
    JButton[] P3_listcard ;
    JButton[] P4_listcard ;

     

	public void init()
	{
		// Take out this line if you don't use symantec.itools.net.RelativeURL or symantec.itools.awt.util.StatusScroller
		//symantec.itools.lang.Context.setApplet(this);

		// This line prevents the "Swing: checked access to system event queue" message seen in some browsers.
		getRootPane().putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE);

		// This code is automatically generated by Visual Cafe when you add
		// components to the visual environment. It instantiates and initializes
		// the components. To modify the code, only use code syntax that matches
		// what Visual Cafe can generate, or Visual Cafe may be unable to back
		// parse your Java file into its visual environment.
		//{{INIT_CONTROLS
		getContentPane().setLayout(null);
		getContentPane().setBackground(java.awt.Color.cyan);
		setSize(236,333);



		cardDisplayLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
		cardDisplayLabel.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
		cardDisplayLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		//cardDisplayLabel.setText("Draw a card...");
		cardDisplayLabel.setOpaque(true);
		getContentPane().add(cardDisplayLabel);
		cardDisplayLabel.setBackground(java.awt.Color.lightGray);
		cardDisplayLabel.setForeground(java.awt.Color.black);
		//cardDisplayLabel.setBounds(48,24,73,97);


               
              
		drawCardBtn.setText("Draw a card...");
		//getContentPane().add(drawCardBtn);
		drawCardBtn.setBounds(24,204,192,24);

		newDeckBtn.setText("New & shuffled deck...");
		//getContentPane().add(newDeckBtn);
		newDeckBtn.setBounds(24,240,192,24);

		cardsLeftLbl.setText("Cards Left:");
		//getContentPane().add(cardsLeftLbl);
		cardsLeftLbl.setForeground(java.awt.Color.black);
		cardsLeftLbl.setBounds(24,276,193,24);

		//}}

		cardDeck = new Deck();






		cardsLeftLbl.setText( "Cards Left: " + cardDeck.size() );

		//{{REGISTER_LISTENERS
		SymAction lSymAction = new SymAction();
		drawCardBtn.addActionListener(lSymAction);
		newDeckBtn.addActionListener(lSymAction);
		//}}
                  final Random rng = new Random();

                  try {
                        HumanHeartsPlayer m = new HumanHeartsPlayer("m");
                        RandomHeartsPlayer r1 = new RandomHeartsPlayer("r1", rng);
                        RandomHeartsPlayer r2 = new RandomHeartsPlayer("r1", rng);
                        RandomHeartsPlayer r3 = new RandomHeartsPlayer("r1", rng);

                        HeartsGame game = new HeartsGame(new LinkedHashSet<HeartsPlayer>(Arrays.asList(new HeartsPlayer[]{
                        m, r1, r2, r3})), rng);

                        final HeartsRound round = new HeartsRound(game.getGameState());
                        round.dealCardsToPlayers();

                        P1_listcard = new JButton[m.getHand().size()];
                        P2_listcard = new JButton[r1.getHand().size()];
                        P3_listcard = new JButton[r2.getHand().size()];
                        P4_listcard = new JButton[r3.getHand().size()];

                        for(int i = 0;i < m.getHand().size();i++)
                        {
                            Card card = m.getHand().get(i);
                            P1_listcard[i] = new JButton();
                            P1_listcard[i].setOpaque(true);
                            P1_listcard[i].setBackground(java.awt.Color.lightGray);
                            P1_listcard[i].setForeground(java.awt.Color.black);
                            P1_listcard[i].setIcon(card.getCardImage());
                            getContentPane().add(P1_listcard[i]);
                            P1_listcard[i].setBounds(140 + 45*i,540,73,97);
                            P1_listcard[i].addActionListener(lSymAction);


                            card = r1.getHand().get(i);
                            P2_listcard[i] = new JButton();
                            P2_listcard[i].setOpaque(true);
                            P2_listcard[i].setBackground(java.awt.Color.lightGray);
                            P2_listcard[i].setForeground(java.awt.Color.black);
                            P2_listcard[i].setIcon(card.getCardImage());
                            getContentPane().add(P2_listcard[i]);
                            P2_listcard[i].setBounds(48 ,120 + 25*i,73,97);
                            P2_listcard[i].addActionListener(lSymAction);
                            
                            card = r2.getHand().get(i);
                            P3_listcard[i] = new JButton();
                            P3_listcard[i].setOpaque(true);
                            P3_listcard[i].setBackground(java.awt.Color.lightGray);
                            P3_listcard[i].setForeground(java.awt.Color.black);
                            P3_listcard[i].setIcon(card.getCardImage());
                            getContentPane().add(P3_listcard[i]);
                            P3_listcard[i].setBounds(140 + 45*i, 30, 73, 97);
                            P3_listcard[i].addActionListener(lSymAction);
                            
                            card = r3.getHand().get(i);
                            P4_listcard[i] = new JButton();
                            P4_listcard[i].setOpaque(true);
                            P4_listcard[i].setBackground(java.awt.Color.lightGray);
                            P4_listcard[i].setForeground(java.awt.Color.black);
                            P4_listcard[i].setIcon(card.getCardImage());
                            getContentPane().add(P4_listcard[i]);
                            P4_listcard[i].setBounds(860 ,120 + 25*i,73,97);
                            P4_listcard[i].addActionListener(lSymAction);

                        }
                        System.out.print("sss");

                        round.passThree();
                       // game.play();
                    }
                    catch (Exception e) {
                    }
      
	}

	//{{DECLARE_CONTROLS

	//}}

	class SymAction implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{
                        for(int i = 0;i < P3_listcard.length;i++)
                        {
                            if(event.getSource() == P3_listcard[i])
                                cardBtn_actionPerformed(event, i);
                        }
			if (event.getSource() == drawCardBtn)
				drawCardBtn_actionPerformed(event);
			else if (event.getSource() == newDeckBtn)
				newDeckBtn_actionPerformed(event);
		}
	}

        void cardBtn_actionPerformed(ActionEvent e, int i){
             System.out.println(P3_listcard[i].getIcon().toString());
        }


	void drawCardBtn_actionPerformed(ActionEvent event)
	{
		Card card = cardDeck.draw();
		//cardDisplayLabel.setText( card.toString() );
       		cardDisplayLabel.setIcon(card.getCardImage());
		cardsLeftLbl.setText( "Cards Left: " + cardDeck.size() );
		if ( cardDeck.isEmpty() )
		    drawCardBtn.setEnabled( false );
	}



	void newDeckBtn_actionPerformed(ActionEvent event)
	{
		//cardDeck.restoreDeck();
                cardDeck = new Deck();
                Random rng = new Random();
		cardDeck.shuffle(rng);
		drawCardBtn.setEnabled( true );
		cardDisplayLabel.setIcon( null );
		//cardDisplayLabel.setText( "Draw a card..." );
		cardsLeftLbl.setText( "Cards Left: " + cardDeck.size() );
	}


}
